<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>demo</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">


    <link rel="stylesheet" href="../../../static/layui2/css/layui.css" media="all"/>
    <link rel="stylesheet" href="../../../static/css/font_eolqem241z66flxr.css" media="all"/>
    <link rel="stylesheet" href="../../../static/css/user.css" media="all"/>
</head>
<body class="childrenBody">


<div style=" position: relative;">
    <blockquote class="layui-elem-quote news_search">
        <div class="layui-inline buttonTable">
            <div class="layui-input-inline">
                <input type="text"  name="name" value="" placeholder="请输入关键字" class="layui-input search_input">
            </div>
            <a class="layui-btn search_btn" data-type="reload"   >查询</a>
        </div>
        <div class="layui-inline buttonTable">
            <a class="layui-btn layui-btn-normal newsAdd_btn" data-type="newsAdd_btn"  >添加文章</a>
        </div>

        <div class="layui-inline buttonTable">
            <a class="layui-btn layui-btn-danger batchDel" data-type="batchDel"   >批量删除</a>
        </div>


    </blockquote>

    <table class="layui-table" id="dataTable" lay-filter="dataTable"></table>
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

</div>
<script type="text/javascript" src="../../../static/layui2/layui.js"></script>
<!--	<script type="text/javascript" src="demoList2.js"></script>-->

<script>
    layui.use(['jquery', 'table', 'form', 'layer', 'laydate','upload'], function () {
        var table = layui.table,
            form = layui.form,
            laydate = layui.laydate,
            upload = layui.upload,
            $ = layui.jquery;

        var active = {
            //查询加载
            reload: function () {

                var name = $('input[name=name]').val();

                table.reload('dataTable', {
                    where: {
                        name: name
                    }
                });
            },



            //新增按钮
            newsAdd_btn: function () {
                var index = layui.layer.open({
                    title : "添加文章",
                    type : 2,
                    content : "appliancesAdd.html?type=add",
                    success : function(layero, index){
                        layui.layer.tips('点击此处返回列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }
                })
                //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
                $(window).resize(function(){
                    layui.layer.full(index);
                })
                layui.layer.full(index);

            },
            //批量删除
            batchDel: function () {

                var ids="";

                var data = layui.table.checkStatus('dataTable').data;
                //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
                if(data.length==0){
                    layer.confirm('请选中要删除的数据', {offset: '150px'}, function (index) {
                        layer.close(index);
                    });
                   /* layer.msg("请选中要删除的数据", function () {
                    });*/
                }else{
                    data.forEach(function(item,index) {
                        var id=item.id;
                        if(index==0){
                            ids=id;
                        }else{
                            ids+=","+id;
                        }

                    });
                    layer.confirm('确认删除选中的数据吗', {offset: '150px'}, function (index) {
                        layer.close(index);
                        $.ajax({
                            url: "/AppliancesController/dels",
                            data: {ids: ids},
                            dataType: "json",
                            type: "post",
                            async: false,
                            success: function (res) {

                                if (res.code === "00") {
                                    active.reload();
                                    form.render();
                                } else if (res.code === "01") {
                                    layer.msg(res.msg, function () {
                                    });
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                console.error(XMLHttpRequest.status);
                                console.error(XMLHttpRequest.readyState);
                                console.error(textStatus);
                            }
                        });
                    });


                }


            },

        };

        $('.buttonTable .layui-btn').on('click', function () {

            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


        laydate.render({
            elem: '#dataDate'
        });

        //分页数据
        table.render({
            elem: '#dataTable'
            , url: '/AppliancesController/getPageDemoList'
            , cellMinWidth: 80
            , height: 'full-100'
            , cols: [[
                {type: 'numbers'}
                 ,{type: 'checkbox'}
                , {field: 'title', align: 'center', width: '15%', title: '标题'}
                , {field: 'price', align: 'center', width: '10%', title: '总价'}
                , {field: 'contacts_phone', align: 'center', width: '15%', title: '手机号'}
                , {field: 'contacts_name', align: 'center', width: '15%', title: '联系人'}
                , {field: 'adress', align: 'center', width: '15%', title: '地址'}
                , {field: 'create_time', align: 'center', width: '15%', title: '时间'}
                , {fixed: 'right', width: 200, align: 'center', title: '操作', toolbar: '#barDemo'}
            ]]

            , groups: 1 //只显示 1 个连续页码
            , first: false //不显示首页
            , last: false //不显示尾页
            , page: true
            , limit: 10
            , limits: [10, 20, 50, 100]
        });

        //监听工具
        table.on('tool(dataTable)', function (obj) {

            var data = obj.data;
            if (obj.event === 'del') {
                layer.confirm('确认删除此条数据吗', {offset: '150px'}, function (index) {
                    layer.close(index);
                    $.ajax({
                        url: "/AppliancesController/del",
                        data: {id: data.id},
                        dataType: "json",
                        type: "post",
                        async: false,
                        success: function (res) {

                            if (res.code === "00") {
                                active.reload();
                                form.render();
                            } else if (res.code === "01") {
                                layer.msg(res.msg, function () {
                                });
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            console.error(XMLHttpRequest.status);
                            console.error(XMLHttpRequest.readyState);
                            console.error(textStatus);
                        }
                    });
                });
            } else if (obj.event === 'edit') {

                var index = layui.layer.open({
                    title : "修改demo",
                    type : 2,
                    content : "appliancesAdd.html?type=edit&id=" + data.id,
                    success : function(layero, index){
                        layui.layer.tips('点击此处返回demo列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    }
                })
                //改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
                $(window).resize(function(){
                    layui.layer.full(index);
                })
                layui.layer.full(index);

            }
        });

    });
</script>
</body>
</html>